import pandas as pd

# 显示全体数据
pd.set_option('display.max_columns', None)


# 读取csv,excel
def csv_read2(path, clas):
    df_pre = pd.DataFrame()
    df_after = pd.DataFrame()
    list_df = []

    rest_num = 0
    row_num = 0
    mark = 1

    # 计算是否有空行
    f = open(path, encoding='gbk')
    str1 = f.readline()
    f.close()
    start_num = 0 if str1.startswith('设备型号') else 1

    df1 = pd.read_csv(path, encoding='gbk', header=None, usecols=[0, 1, 2])
    index1 = df1[df1[0] == 'Cpk, Cp'].index.values[0]

    df_on = df1.loc[1:index1, :3]
    sql_cols = ['TestDate', 'TestTime', 'TestNo', 'Operator', 'Class', 'MachineID', 'WireDiameter', 'ChipSize',
                'DB_glue',
                'Operator', 'LoadUnit', 'Sensor', 'TestMethod', 'AcceptanceVlaue', 'TestingSpeed', 'ShearHeight']

    # 将上半部分的信息转换威df
    for i in range(len(sql_cols)):
        df_pre.loc[0, sql_cols[i]] = df_on.iloc[i, 1]

    # 读取测试的值，并对列进行命名
    df_down = pd.read_csv(path, encoding='gbk', header=None, usecols=[0, 1, 2, 3], skiprows=index1 + start_num + 1)
    df_down = df_down.reset_index(drop=True)
    # print(df_down)

    if clas in ('焊线拉力-齐纳') and 'B点断芯片打芯片' in set(list(df_down[2])):
        if len(df_down) % 8 == 0:
            for i in range(len(df_down)):
                j = i % 8
                if j == 0 or j == 1 or j == 2 or j == 3:
                    df_after.loc[0, f'{"B_open_chip_to_chip" + str(mark)}'] = round(df_down.loc[i, 3], 2)
                elif j == 4 or j == 5:
                    df_after.loc[0, f'{"B_open_chip_to_bonding" + str(mark)}'] = round(df_down.loc[i, 3], 2)
                elif j == 6 or j == 7:
                    df_after.loc[0, f'{"QN_open_chip_to_chip" + str(mark)}'] = round(df_down.loc[i, 3], 2)
                if mark == 40 or i == len(df_down) - 1:
                    list_df.append(pd.concat([df_pre, df_after], axis=1))
                    mark = 0
                mark += 1
        else:
            rest_num = len(df_down) % 8
        return df_down, list_df, row_num, rest_num

    elif clas in ('焊线推力-齐纳') and '正极推力' in set(list(df_down[2])):
        if len(df_down) % 10 == 0:
            for i in range(len(df_down)):
                j = i % 10
                if j == 0 or j == 1:
                    df_after.loc[0, f'{"P_shear" + str(mark)}'] = round(df_down.loc[i, 3], 2)
                elif j == 2 or j == 3:
                    df_after.loc[0, f'{"N_shear" + str(mark)}'] = round(df_down.loc[i, 3], 2)
                elif j == 4 or j == 5:
                    df_after.loc[0, f'{"Bonding_shear" + str(mark)}'] = round(df_down.loc[i, 3], 2)
                elif j == 6 or j == 7:
                    df_after.loc[0, f'{"QN_P_shear" + str(mark)}'] = round(df_down.loc[i, 3], 2)
                elif j == 8 or j == 9:
                    df_after.loc[0, f'{"QN_N_shear" + str(mark)}'] = round(df_down.loc[i, 3], 2)
                if mark == 50 or i == len(df_down) - 1:
                        list_df.append(pd.concat([df_pre, df_after], axis=1))
                        mark = 0
                mark += 1
        else:
            rest_num = len(df_down) % 10
        return df_down, list_df, row_num, rest_num
    else:
        # print('请勾选数据类型')
        return None, '数据类型勾选错误', None, None
